package com.laituo.juyou.app.api.recommend;

import com.laituo.juyou.biz.service.recommend.RecommendBizService;
import com.laituo.juyou.core.exception.ServiceException;
import com.laituo.juyou.data.dto.RecommendDTO;
import com.laituo.juyou.data.dto.RecommendGoodDTO;
import com.laituo.juyou.data.mapper.RecommendMapper;
import com.laituo.juyou.data.model.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: kbq
 * Date: 2019-07-08
 * Time: 下午3:40
 *
 * @author kaixin
 */
@Service
public class RecommendServiceImpl implements RecommendService {

    @Resource
    private RecommendBizService recommendBizService;

    @Resource
    private RecommendMapper recommendMapper;

    @Override
    public Page<RecommendDTO> getRecommendByStorage(Long storageId, Integer recommendType, Integer pageNo, Integer pageSize) throws ServiceException {
        return recommendBizService.getRecommendByType(storageId, recommendType, pageNo, pageSize);
    }

    @Override
    public RecommendGoodDTO getRecommendByStorageAndId(Long storageId, Integer recommendType, Integer id) throws ServiceException {
        //单个商品信息
        RecommendGoodDTO recommendGooDTO = new RecommendGoodDTO();
        List<RecommendDTO> recommendList = recommendMapper.getRecommendByStorageAndId(storageId, recommendType);
        if (id == -1) {
            recommendGooDTO.setRecommendDTO(recommendList.get(0));
            recommendList.remove(0);
        } else {
            RecommendDTO recommend = recommendMapper.getRecommendById(id, storageId, recommendType);
            recommendGooDTO.setRecommendDTO(recommend);
            int index = recommendList.indexOf(recommend);
            if (index >= 0) {
                recommendList.remove(index - 1);
            }
        }
        //为你推荐
        recommendGooDTO.setRecommendDTOList(recommendList);
        return recommendGooDTO;
    }
}
